package com.lanren.familybookapp.ui.me.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.common.utils.GsonUtils;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IBookEditModel;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName BookEditModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/2/12 20:12
 * @Version 1.0
 */
public class BookEditModel extends BaseModel {
    public BookEditModel() {
        initDispoable();
    }

    public void updateBook(int bookId, String bookName, String bookBudget, String userInvite, final IBookEditModel.BookEditModel
            bookEditModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        map.put("bookName", bookName);
        map.put("bookBudget", bookBudget);
        map.put("userInvite", userInvite);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_UPDATE)
                .headers(AppUserHelper.getHttpHeaders())
                .upJson(GsonUtils.toJson(map))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            bookEditModel.onSuccess(e.getMessage());
                        } else {
                            bookEditModel.onError(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        bookEditModel.onSuccess(s);
                    }
                });
        addDisposable(disposable);
    }

    //获取账本详细
    public void getBookInfo(int bookId, final IBookEditModel.GetBookEditModel getBookEditModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_LIST)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookInfoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        getBookEditModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<BookInfoBean> bookInfoBeans) {
                        getBookEditModel.onSuccess(bookInfoBeans);

                    }
                });
        addDisposable(disposable);

    }
}
